package com.ocean.web.controller.ftp;

import com.ocean.common.annotation.Log;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.core.page.TableDataInfo;
import com.ocean.common.enums.BusinessType;
import com.ocean.common.utils.ShiroUtils;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.utils.uuid.IdUtils;
import com.ocean.system.domain.ODownOrder;
import com.ocean.system.domain.SysFTPConfigLeftJoinUser;
import com.ocean.system.domain.SysFTPConfigftp;
import com.ocean.system.domain.SysFTPLogftp;
import com.ocean.system.mapper.SysPostMapper;
import com.ocean.system.multithreading.DownloadFileTask;
import com.ocean.system.service.IODownOrderService;
import com.ocean.system.service.ISysPostService;
import com.ocean.system.service.ISysRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * FTP日志 控制层
 * @author ocean
 */
@Controller
@RequestMapping("/system/record")
public class SysRecordController extends BaseController
{
    private String prefix = "system/record";
    @Autowired
    private SysPostMapper postMapper;
    @Autowired
    private ISysRecordService recordService;

    @Autowired
    private IODownOrderService downOrderService;
    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/record";
    }

    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFTPLogftp post)
    {
        startPage();
        List<SysFTPLogftp> list = null;
        SysUser currentUser = ShiroUtils.getSysUser();
        if ("admin,管理员".contains(currentUser.getLoginName())){
            list = recordService.selectRecordList(post);
        }else{
            post.setUserId(currentUser.getUserId().toString());
            list = recordService.selectRecordList(post);
        }
        //List<SysFTPLogftp> list = recordService.selectRecordList(post);
        return getDataTable(list);
    }

    @Log(title = "导出数据推送统计表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:record:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysFTPLogftp post)
    {
        List<SysFTPLogftp> list = recordService.selectRecordList(post);
        List<SysFTPLogftp> newList = new ArrayList<>();
        for (SysFTPLogftp sysFTPLog : list) {
            if(newList.size()<11){
                newList.add(sysFTPLog);
            }
        }
        ExcelUtil<SysFTPLogftp> util = new ExcelUtil<>(SysFTPLogftp.class);
        return util.exportExcel(newList, "数据推送统计表");
    }

    /**
     * 手动推送
     * @param
     * @return
     */
    @PostMapping("/sdts")
    @ResponseBody
    public AjaxResult sdts(SysFTPLogftp sysFTPLogftp){
        SysFTPLogftp sysFTPLogftps = postMapper.getFTPlogById(sysFTPLogftp.getId());
        ODownOrder oDownOrder = downOrderService.selectODownOrderById(sysFTPLogftps.getDownOrderId());
        if(oDownOrder!=null){
            ODownOrder down = new ODownOrder();
            String downId = IdUtils.simpleUUID();
            down.setId(downId);
            down.setDownNo(String.valueOf(System.currentTimeMillis()));
            down.setResourcePath(sysFTPLogftps.getResourcePath());
            down.setWorkNo(oDownOrder.getWorkNo());
            down.setUserId(oDownOrder.getUserId());
            down.setStatus("0");
            down.setCreateDate(new Date());
            down.setDepartId(oDownOrder.getDepartId());
            down.setVirtualPath("/down/" + downId);
            int i = downOrderService.insertODownOrder(down);
            sysFTPLogftps.setStatus("3");
            postMapper.upFTPLog(sysFTPLogftps);//修改状态
            return AjaxResult.success("已重新将文件转移至推送列表,十分钟后刷新推送日志页面查看推送状态");
        }
        return AjaxResult.error("未找到推送下载单数据");
    }

}
